package com.xinxin.siteset.service.impl;

import com.xinxin.siteset.dao.ClassificationMapper;
import com.xinxin.siteset.dao.SiteMapper;
import com.xinxin.siteset.dto.ClassificationDTO;
import com.xinxin.siteset.dto.SiteDTO;
import com.xinxin.siteset.model.Classification;
import com.xinxin.siteset.model.Site;
import com.xinxin.siteset.service.ClassificationService;
import com.xinxin.siteset.service.CommonService;
import com.xinxin.siteset.service.SiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 史鑫鑫
 * @date Created in 2019/6/7 1:30
 */
@Service
public class CommonServiceImpl implements CommonService {
    @Autowired
    ClassificationMapper classificationMapper;
    @Autowired
    SiteMapper siteMapper;

    @Override
    public List<ClassificationDTO> getAllSites() {
        List<ClassificationDTO> allSiteList = new ArrayList<>();
        // 获取所有分类名
        List<Classification> classificationList = classificationMapper.selectAll();

        for (Classification classification : classificationList) {
            // 获取该分类名下的所有网站
            List<Site> siteList = siteMapper.selectByClassificationId(classification.getId());
            List<SiteDTO> siteDTOList = new ArrayList<>();
            for (Site site : siteList) {
                // 设置属性
                SiteDTO siteDTO = new SiteDTO();
                siteDTO.setId(site.getId());
                siteDTO.setSiteUrl(site.getSiteUrl());
                siteDTO.setSiteName(site.getSiteName());
                siteDTO.setSiteTitle(site.getSiteTitle());
                // 添加
                siteDTOList.add(siteDTO);
            }
            // 设置属性
            ClassificationDTO classificationDTO = new ClassificationDTO();
            classificationDTO.setId(classification.getId());
            classificationDTO.setClassificationName(classification.getClassificationName());
            classificationDTO.setSiteDTOList(siteDTOList);
            // 添加
            allSiteList.add(classificationDTO);
        }
        return allSiteList;
    }
}
